<template>
  <div class="setting">
    <el-card>
      <el-tabs>
        <el-tab-pane label="角色管理">
          <el-button
            type="primary"
            class="btn"
            @click="addRoleOpen"
          >+ 新增角色</el-button>
          <el-table :data="list" border>
            <el-table-column label="序号" width="80px">
              <template v-slot="{ $index }">
                <div>
                  {{ (page.page - 1) * page.pagesize + $index + 1 }}
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="name" label="角色名" sortable />
            <el-table-column prop="description" label="描述" sortable />
            <el-table-column label="操作">
              <template v-slot="{ row }">
                <el-button
                  type="text"
                  @click="permissionClick(row)"
                >分配权限</el-button>
                <el-button type="text" @click="edit(row)">修改</el-button>
                <el-button type="text" @click="del(row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
          <div class="page">
            <el-pagination
              :current-page="page.page"
              :page-sizes="[1, 2, 10, 100, 200, 300, 400]"
              :page-size="page.pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="page.total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </div>
        </el-tab-pane>
        <el-tab-pane label="公司信息" />
      </el-tabs>
    </el-card>
    <!-- 新增弹框组件 -->
    <!-- <Add :dialog-visible="show" @input="show = $event" /> -->
    <Add ref="add" v-model="show" @getData="getData" />
    <!-- 分配权限弹框组件 -->
    <!-- <SetPermission
      :visible-show="permissionShow"
      @setPermission="permissionShow = $event"
    /> -->
    <SetPermission ref="setPermission" v-model="permissionShow" />
  </div>
</template>

<script>
import { sysRole, sysRoleDelete } from '@/api/setting'
import cookie from 'js-cookie'
import Add from './components/add.vue'
import SetPermission from './components/SetPermission.vue'
export default {
  components: {
    Add,
    SetPermission
  },
  data() {
    return {
      list: [],
      page: {
        page: 1,
        pagesize: +cookie.get('settingPageSize') || 2, // 隐式转换
        total: 100
      },
      show: false,
      permissionShow: false
    }
  },
  created() {
    this.getData()
  },
  methods: {
    async getData() {
      const res = await sysRole(this.page)
      this.list = res.data.rows
      this.page.total = res.data.total
    },
    handleSizeChange(size) {
      this.page.pagesize = size
      cookie.set('settingPageSize', size)
      this.page.page = 1
      this.getData()
    },
    handleCurrentChange(page) {
      this.page.page = page
      this.getData()
    },
    addRoleOpen() {
      this.show = true
      this.$refs.add.mode = 'add'
    },
    edit(row) {
      this.show = true
      // 数据回显
      this.$refs.add.form = row
      // 修改场景值
      this.$refs.add.mode = 'edit'
    },
    del(row) {
      this.$confirm('你确定删除该条数据吗', '提示')
        .then(async() => {
          await sysRoleDelete(row.id)
          this.$message.success('删除数据成功')
          this.page.page = 1
          this.getData()
        })
        .catch(() => {})
    },
    // 权限点击
    async permissionClick(row) {
      this.permissionShow = true
      // 调用子组件内获取当前角色的权限点数据的方法
      this.$refs.setPermission.getRoleInfo(row.id)
    }
  }
}
</script>

<style lang="scss" scoped>
.setting {
  padding: 20px;
  .btn {
    margin: 20px 0;
  }
  .page {
    text-align: right;
    margin-top: 40px;
  }
}
</style>
